import { Toast } from 'mint-ui';
import comfirmLogin from '@/components/comfirm.vue';
export default {
  name: "",
  props: ['billMoney','billId','contractId','seq'],
  data: () => ({
    comfirmTitle:{
      comfirmTop:'提示',
      comfirmCon:'支付成功',
      comfirmBtn:['取消','确认'],
    },
    showComfirm:false,
    isPayClick:true,    //支付按钮可点击,false为不可多次点击
  }),
  components: {
    comfirmLogin,
  },
  mounted() {
    //do something after mounting vue instance
    console.log(this.billMoney);
  },
  methods: {
    closePayfor() {
      this.$emit('closePayfor');
    },
    // 支付
    submitPayfor() {
      let that = this;
      if(that.isPayClick){    //用户第一次点击请求支付
        that.isPayClick = false;    //关闭请求支付按钮
        let payforParam = {
          billId:that.billId
        }
        network.post(configUrl.BILLWXPAY_URL,payforParam,function (res) {
          console.log(res);
          let data = res.data;
          // 调用微信支付
          function onBridgeReady(){
             // console.log('开始支付');
            WeixinJSBridge.invoke(
              'getBrandWCPayRequest', {
                "appId" : data.appId,   //公众号名称，由商户传入
                "timeStamp":data.timeStamp,     //时间戳，自1970年以来的秒数
                "nonceStr" : data.nonceStr, //随机串
                "package" : data.package,   //统一订单号
                "signType" : data.signType,     //微信签名方式：
                "paySign" : data.paySign //支付签名
              },
              function(res){
                console.log((res.err_msg));
                if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                  // alert('支付成功'+res.err_msg);
                  // that.$router.push({path:'/payforSuccess',query:{contractId:that.contractId}});
                  that.showComfirm = true;
                }   // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                else{
                  Toast('支付失败');
                  that.isPayClick = true;
                }
              }
            );
          }
          if (typeof WeixinJSBridge == "undefined"){

            if( document.addEventListener ){
              Toast('请使用微信浏览器')
              document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
            }else if (document.attachEvent){
              document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
              document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
            }
          }else{
            onBridgeReady();
          }
          /////微信支付end
        },function (err) {
          console.log(err);
        })
      }else{
        console.log('您已请求支付，不可重复点击');
      }
    },
    comfirmEvent(data) {
      console.log(data);
      let that = this;
      console.log(that.contractId);
      if(that.seq==1){    //第一期账单需要跳转到合同
        that.$router.push({path:'/payforSuccess',query:{contractId:that.contractId}});
      }else {   //其他期的账单付款成功后直接回退到账单列表
        that.$router.go(-2);
      }

      that.showComfirm = false;
    }
  }
}
